from django.db import models


class StorageInstance(models.Model):
    """
    存储实例
    """
    name = models.CharField(
        default='',
        max_length=50,
        verbose_name='名称'
    )
    type = models.IntegerField(
        default=1,
        choices=(
            (1, 'volume'),
            (2, 'oss')
        ),
        verbose_name='类型'
    )
    endpoint = models.CharField(
        default='',
        max_length=200,
        verbose_name='endpoint',
        blank=True,
    )
    accessKeyID = models.CharField(
        default='',
        max_length=200,
        verbose_name='accessKeyID',
        blank=True,
    )
    accessKeySecret = models.CharField(
        default='',
        max_length=200,
        verbose_name='accessKeySecret',
        blank=True,
    )

    class Meta:
        verbose_name = "存储实例"
        verbose_name_plural = "存储实例 - (StorageInstance)"

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        super(StorageInstance, self).save()
        create_uid = 0
        if 'create_uid' in kwargs:
            create_uid = kwargs['create_uid']
        from record.models import StorageInstanceRecord
        obj = StorageInstanceRecord.objects.create(
            storage_instance_id=self.id,
            name=self.name,
            type=self.type,
            endpoint=self.endpoint,
            accessKeyID=self.accessKeyID,
            accessKeySecret=self.accessKeySecret,
            create_uid=create_uid,
        )


class DBInstance(models.Model):
    """
    DB实例
    """
    name = models.CharField(
        default='',
        max_length=50,
        verbose_name='name'
    )
    host = models.CharField(
        default='',
        max_length=50,
        verbose_name='host'
    )
    username = models.CharField(
        default='',
        max_length=50,
        verbose_name='username'
    )
    password = models.CharField(
        default='',
        max_length=50,
        verbose_name='password'
    )
    database_name = models.CharField(
        default='',
        max_length=50,
        verbose_name='database_name'
    )

    class Meta:
        verbose_name = "DB实例"
        verbose_name_plural = "DB实例 - (DBInstance)"

    def __str__(self):
        return self.name

    def save(self, *args, **kwargs):
        super(DBInstance, self).save()
        create_uid = 0
        if 'create_uid' in kwargs:
            create_uid = kwargs['create_uid']
        from record.models import DBInstanceRecord
        obj = DBInstanceRecord.objects.create(
            db_instance_id=self.id,
            name=self.name,
            host=self.host,
            username=self.username,
            password=self.password,
            database_name=self.database_name,
            create_uid=create_uid,
        )
